package com.mall.client.server.service.impl;

import java.util.List;

import com.mall.client.server.domain.*;
import com.mall.client.server.domain.vo.ClientVo;
import com.mall.client.server.domain.vo.FreezeVo;
import com.mall.client.server.domain.vo.VipVo;
import com.mall.client.server.mapper.ClientMapper;
import com.mall.client.server.service.IClientService;
import com.mall.common.domain.exceptions.BizException;
import com.mall.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 客户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-16
 */
@Service
public class ClientServiceImpl implements IClientService
{
    @Autowired
    private ClientMapper clientMapper;

    /**
     * 查询客户
     *
     * @param id 客户主键
     * @return 客户
     */
    @Override
    public Client selectClientById(Integer id)
    {
        return clientMapper.selectClientById(id);
    }

    /**
     * 查询客户列表
     *
     * @param client 客户
     * @return 客户
     */
    @Override
    public List<ClientVo> selectClientList(Client client)
    {
//        return clientMapper.selectClientList(client);
        List<ClientVo> clients = clientMapper.selectClientList(client);
        return clients;
    }

    /**
     * 新增客户
     *
     * @param client 客户
     * @return 结果
     */
    @Override
    public int insertClient(Client client)
    {
        //先添加主表
        int i = clientMapper.insertClient(client);
        Integer id = client.getId();
        Integer[] clientTagId = client.getClientTagId();
        //后添加中间表数据
        clientMapper.middleAdds(id,clientTagId);
        return i;
    }

    /**
     * 修改客户
     *
     * @param client 客户
     * @return 结果
     */
    @Override
    public int updateClient(Client client)
    {
        return clientMapper.updateClient(client);
    }

    /**
     * 批量删除客户
     *
     * @param ids 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteClientByIds(Integer[] ids)
    {
        return clientMapper.deleteClientByIds(ids);
    }

    /**
     * 删除客户信息
     *
     * @param id 客户主键
     * @return 结果
     */
    @Override
    public int deleteClientById(Integer id)
    {
        return clientMapper.deleteClientById(id);
    }

    @Override
    public void userCouponAdd(CouponIdsAndUserId couponIdsAndUserId) {
        // 1. 拆分对象
        String couponIds = couponIdsAndUserId.getCouponIds();
        String userIds = couponIdsAndUserId.getCustomerIds();
        if (couponIds == null || couponIds.trim().isEmpty()) {
            throw new BizException("优惠券ID不能为空");
        }
        if (userIds == null || userIds.trim().isEmpty()) {
            throw new BizException("用户ID不能为空");
        }
        // 前台传过来的优惠券ID
        String[] couponIdArray = couponIds.split(",");
        // 前台传过来的用户ID
        String[] userIdArray = userIds.split(",");
        // 验证输入数据的有效性
        if (couponIdArray.length < 1) {
            throw new BizException("请选择优惠券");
        }
        if (userIdArray.length < 1) {
            throw new BizException("请选择要发送的用户");
        }
        // 循环遍历用户列表，并将每个优惠券分配给这些用户
        for (String userId : userIdArray) {
            for (String couponId : couponIdArray) {
                clientMapper.userCouponAdd(userId,couponId);
            }
        }
    }
    @Override
    public void addTag(Client client) {
        if (client.getCustomerIds().size() == 0){
            throw new BizException("未选择客户，无法添加标签");
        }
        Integer id = client.getId();
        Integer[] clientTagId = client.getClientTagId();
        //后添加中间表数据
        clientMapper.middleAdds(id,clientTagId);
    }

    @Override
    public void updateCurrentPoints(Points points) {
        if (points.getCustomerIds().size() == 0){
            throw new BizException("未选择客户，无法添加积分");
        }
        clientMapper.updateCurrentPoints(points);
    }

    @Override
    public List<Coupon> selectCouponList() {
        return clientMapper.selectCouponList();
    }

    @Override
    public void addVipToCustomer(VipVo vipVo) {
        if (vipVo.getCustomerIds().length == 0){
            throw new RuntimeException("未选择客户，无法添加");
        }
        if (vipVo.getVipIds().length == 0){
            throw new RuntimeException("未选择会员等级，无法添加");
        }
        for (Long customerId : vipVo.getCustomerIds()) {

            String vipCard = String.valueOf(RandomUtils.nextInt(100000, 999999));

            vipVo.setVipGrade(1L);
            vipVo.setVipCard(vipCard);
            vipVo.setCustomerId(customerId);
            clientMapper.addVipToCustomer(vipVo);
        }
    }

    @Override
    public List<FreezeVo> selectCmmFreezeAll() {
        return clientMapper.selectFreezeAll();
    }

    @Override
    public void addSvip(Client client) {
        Integer id = client.getId();
        Integer[] svipIds = client.getSvipIds();
        //后添加中间表数据
        clientMapper.middleAddSvipIds(id,svipIds);
    }

    @Override
    public void addClientFreeze(Client client) {
        Integer id = client.getId();
        Integer[] clientFreezeIds = client.getFreezeIds();
        //后添加中间表数据
        clientMapper.middleAddFreezeIds(id,clientFreezeIds);
    }
}
